home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / Miracle C Compiler / miricleC compiler.msi / Instal01.cab / _9852DC4593AD46F89A0EA43EE7E8E15D < prev    next >
Encoding:
Text File  |  2000-07-25  |  8.2 KB  |  198 lines

  1. #include <io.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <assert.h>
  6.  
  7. void xpfsf();
  8.  
  9. int main()
  10. {
  11.     xpfsf();
  12. }
  13.  
  14. #define PANSWER1 "<teststr><a><9876><6789><face><64206>"
  15. #define PANSWER2 "<twoth     ><b  ><9876  >< 06789><  1234><face><FACE><face><64206>"
  16. #define PANSWER3 "<     freef>< c><  -659876><   126789><+1234>< 1234><-1234><  face4321><  FACE9876><  face4290><-1330>"
  17. #define PANSWER4 "<-4201><-12><253><-120368><1762><543213>"
  18. #define PANSWER5 "<1.234500><903.142><903.141600><1.200000e+215><1.200000E-139><2.71828e-006><7.8192E+005><0.000003><781922.301000>"
  19.  
  20. // --- from xmath.c
  21. int roughly(double x, double y)
  22. {
  23.     if(y==0.0)
  24.        return (fabs(x) < 1.0e-6);
  25.     else
  26.        return (fabs((y-x)/y) < 1.0e-6);
  27. }
  28.  
  29.  
  30. void xpfsf()
  31. {
  32.     FILE *fp, tempin, tempout;
  33.     char buf[300], bufa[100], bufb[100], bufc[100], bufd[100];
  34.     int n1,n2,n3,n4,n5,n6,n7;
  35.     short s1,s2;
  36.     unsigned long l1,l2,l3,l4,l5;
  37.     char c1,c2;
  38.     float f1,f2,f3,f4,f5,f6,f7,f8,f9;
  39.  
  40.     // --- test the printf/scanf family of functions
  41.     printf("==> starting xpfsf <==\n");
  42.  
  43.     // --- first we test sprintf
  44.     // --- sprintf/printf/fprintf all use one function for formatting, so we need only test sprintf
  45.     assert(sprintf(buf,"<%s><%c><%d><%u><%x><%u>","teststr",'a',9876,6789,0xface,0xface)==strlen(PANSWER1));
  46.     assert(strcmp(buf,PANSWER1)==0);
  47.  
  48.     assert(sprintf(buf,"<%-10.5s><%-3c><%-6.4d><%6.5u><%6.3u><%x><%X><%x><%u>",
  49.             "twothree",'b',9876,6789,1234,0xface,0xface,0xface,0xface)==strlen(PANSWER2));
  50.     assert(strcmp(buf,PANSWER2)==0);
  51.  
  52.     assert(sprintf(buf,"<%10.5s><%2c><%9.5ld><%+9.5lu><%+d><% d><%+d><%10lx><%10lX><%10lx><%d>",
  53.             "freefour",'c',-659876L,126789L,1234,1234,-1234,0xface4321L,0xface9876L,0xface4290L,0xface)==strlen(PANSWER3));
  54.     assert(strcmp(buf,PANSWER3)==0);
  55.  
  56.     assert(sprintf(buf,"<%i><%hi><%o><%li><%hd><%lo>",-4201,-12,0253,-120368L,1762,0543213L,0234561L)==strlen(PANSWER4));
  57.     assert(strcmp(buf,PANSWER4)==0);
  58.  
  59.     assert(sprintf(buf,"<%f><%2.3f><%f><%e><%E><%g><%10.5G><%f><%f>",
  60.             1.2345,903.1416,903.1416,1.2e215,1.2e-139,0.00000271828,781922.301,0.00000271828,781922.301)==strlen(PANSWER5));
  61.     assert(strcmp(buf,PANSWER5)==0);
  62.     printf("passed sprintf tests...   ");
  63.  
  64.     // --- now we test sscanf
  65.     // --- sprintf/printf/fprintf all use one function for formatting, so we need only test sprintf
  66.     memset(bufa,0,sizeof(bufa)); memset(bufb,0,sizeof(bufb)); memset(bufc,0,sizeof(bufc)); memset(bufd,0,sizeof(bufd)); n1=0;
  67.     strcpy(buf,"hello world mars calling 4201");
  68.     assert(sscanf(buf,"%s%s%s %s %d",bufa,bufb,&bufc[0],&bufd[0],&n1)==5);
  69.     assert(strcmp(bufa,"hello")==0); assert(strcmp(bufb,"world")==0);
  70.     assert(strcmp(bufc,"mars")==0); assert(strcmp(bufd,"calling")==0);
  71.     assert(n1==4201);
  72.  
  73.     n1=n2=s1=c1=c2=0; l1=l2=0L;
  74.     strcpy(buf,"yn-4201;0123face;0123456;cab2fade");
  75.     assert(sscanf(buf,"%c%c%hd;%o%x;%lo;%lx",&c1,&c2,&s1,&n1,&n2,&l1,&l2)==7);
  76.     assert(c1=='y'); assert(c2=='n');
  77.     assert(s1==-4201); assert(n1==0123); assert(n2==0xface);
  78.     assert(l1==42798L); assert(l1==0123456L); assert(l2==3400727262L); assert(l2==0xcab2fadeL);
  79.  
  80.     f1=f2=f3=0.0;
  81.     strcpy(buf,"2.67e-283; 3.14; 2.718e59;");
  82.     assert(sscanf(buf,"%f; %e; %g;",&f1,&f2,&f3)==3);
  83.     assert(fabs(f1-(2.67e-283)) < 1e-200); assert(fabs(f2-3.14)<0.01); assert(fabs(f3-2.718e59) < 1e50);
  84.  
  85.     printf("passed sscanf tests...\n");
  86.  
  87.     // --- test fprintf
  88.     fp = fopen("testdata.txt","w");    // --- open for write and truncate
  89.  
  90.     assert(fp!=NULL && fp>stdprn);
  91.     assert(ferror(fp)==0 && feof(fp)==0);
  92.  
  93.     assert(fprintf(fp,"<%s><%c><%d><%u><%x><%u>\n","teststr",'a',9876,6789,0xface,0xface)==strlen(PANSWER1)+1);
  94.     assert(fprintf(fp,"<%-10.5s><%-3c><%-6.4d><%6.5u><%6.3u><%x><%X><%x><%u>\n",
  95.             "twothree",'b',9876,6789,1234,0xface,0xface,0xface,0xface)==strlen(PANSWER2)+1);
  96.     assert(fprintf(fp,"<%10.5s><%2c><%9.5ld><%+9.5lu><%+d><% d><%+d><%10lx><%10lX><%10lx><%d>\n",
  97.             "freefour",'c',-659876L,126789L,1234,1234,-1234,0xface4321L,0xface9876L,0xface4290L,0xface)==strlen(PANSWER3)+1);
  98.     assert(fprintf(fp,"<%i><%hi><%o><%li><%hd><%lo>\n",-4201,-12,0253,-120368L,1762,0543213L,0234561L)==strlen(PANSWER4)+1);
  99.     assert(fprintf(fp,"<%f><%2.3f><%f><%e><%E><%g><%10.5G><%f><%f>\n",
  100.             1.2345,903.1416,903.1416,1.2e215,1.2e-139,0.00000271828,781922.301,0.00000271828,781922.301)==strlen(PANSWER5)+1);
  101.  
  102.     assert(fclose(fp)==0);
  103.  
  104.     // --- now verify fprintf output, and test fscanf
  105.     fp = fopen("testdata.txt","r");    // --- open for write and truncate
  106.     assert(fp!=NULL && fp>stdprn);
  107.     assert(ferror(fp)==0 && feof(fp)==0);
  108.  
  109.     // --- first line of testdate
  110.     bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=0;
  111.     assert(fscanf(fp,"<%7s><%c><%d><%u><%x><%u> ",bufa,&c1,&n1,&n2,&n3,&n4)==6);
  112.     assert(strcmp(bufa,"teststr")==0); assert(c1=='a');
  113.     assert(n1==9876); assert(n2==6789); assert(n3==0xface); assert(n4==64206);
  114.  
  115.     // --- second line of testdate
  116.     bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=n5=n6=0; l1=0L;
  117.     assert(fscanf(fp,"<%s ><%c ><%d><%u><%u><%x><%X><%x><%u> ",bufa,&c1,&n1,&n2,&n3,&n4,&l1,&n5,&n6)==9);
  118.     assert(strcmp(bufa,"twoth")==0); assert(c1=='b');
  119.     assert(n1==9876); assert(n2==6789); assert(n3==1234); assert(n4==0xface); assert(n5==0xface);
  120.     assert(n6==64206); assert(l1==0xfaceL);
  121.  
  122.     // --- third line of testdate
  123.     bufa[0]='\0'; c1='Y'; n1=n2=n3=n4=0; l1=l2=l3=l4=l5=0L;
  124.     assert(fscanf(fp,"< %5s>< %c><%ld><%lu><%d>< %d><%d>< %lx>< %lX>< %lx><%d> ",bufa,&c1,&l1,&l2,&n1,&n2,&n3,&l3,&l4,&l5,&n4)==11);
  125.  
  126.     assert(strcmp(bufa,"freef")==0); assert(c1=='c');
  127.     assert(n1==1234); assert(n2==1234); assert(n3==-1234); assert(n4==-1330);
  128.     assert(l1==-659876L); assert(l2==126789L); assert(l3==0xface4321L); assert(l4==0xFACE9876L); assert(l5==0xface4290L);
  129.  
  130.     // --- fourth line of testdate
  131.     n1=n2=0; s1=s2=0; l1=l2=0L;
  132.     assert(fscanf(fp,"<%i><%hi><%o><%li><%hd><%lo> ",&n1,&s1,&n2,&l1,&s2,&l2)==6);
  133.  
  134.     assert(n1==-4201); assert(s1==-12); assert(n2==0253); assert(l1==-120368L);
  135.     assert(s2==1762); assert(l2==0543213L);
  136.  
  137.     // --- fifth line of testdate
  138.     f1=f2=f3=f4=f5=f6=f7=f8=f9=0.0;
  139.     assert(fscanf(fp,"<%f><%e><%g><%f><%e><%g><%f><%e><%f> ",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9)==9);
  140.     assert(roughly(f1,1.2345));    assert(roughly(f2,903.142));    assert(roughly(f3,903.141600));
  141.     assert(roughly(f4,1.2e215));    assert(roughly(f5,1.2e-139));    assert(roughly(f6,0.00000271828));
  142.     assert(roughly(f7,7.8192E+005));assert(roughly(f8,0.000003));    assert(roughly(f9,781922.301));
  143.  
  144.     printf("passed fprintf/fscanf tests...   ");
  145.     assert(fclose(fp)==0);
  146.  
  147.  
  148.     // --- test scanf/printf from/to stdin/stdout
  149.     // --- test printf
  150.  
  151.     // --- note that assert() writes to stdout
  152.     // --- so you should check the "testout.txt" file for assertion failure
  153.     fp = fopen("testdata.txt","w");    // --- open for write and truncate
  154.  
  155.     assert(fp!=NULL && fp>stdprn);
  156.     memcpy(&tempout,stdout,sizeof(FILE));
  157.     memcpy(stdout,fp,sizeof(FILE));    
  158.  
  159.     printf("%s %s %d\n","hello world","mars calling",4201);
  160.     printf("%c%c%hd;%o%x;%lo;%lx\n",'y','n',-4201,0123,0xface,0123456L,0xcab2fadeL);
  161.     printf("%g; %f; %g;\n",2.67e-283,3.14,2.718e59);
  162.  
  163.     assert(fclose(stdout)==0);
  164.     memcpy(stdout,&tempout,sizeof(FILE));    // --- return stdout to previous settings
  165.  
  166.  
  167.     // --- now we read some bytes from the file
  168.     fp = fopen("testdata.txt","r");
  169.     // --- now we reassign stdin and issue standard i/o calls
  170.     memcpy(&tempin,stdin,sizeof(FILE));
  171.     assert(fp!=NULL && fp>stdprn);
  172.     memcpy(stdin,fp,sizeof(FILE));
  173.  
  174.     memset(bufa,0,sizeof(bufa)); memset(bufb,0,sizeof(bufb));
  175.     memset(bufc,0,sizeof(bufc)); memset(bufd,0,sizeof(bufd)); n1=0;
  176.  
  177.     assert(scanf("%s%s%s %s %d",bufa,bufb,&bufc[0],&bufd[0],&n1)==5);
  178.     assert(strcmp(bufa,"hello")==0); assert(strcmp(bufb,"world")==0);
  179.     assert(strcmp(bufc,"mars")==0); assert(strcmp(bufd,"calling")==0);
  180.     assert(n1==4201);
  181.  
  182.     n1=n2=s1=c1=c2=0; l1=l2=0L;
  183.     assert(scanf("%c%c%hd;%o%x;%lo;%lx",&c1,&c2,&s1,&n1,&n2,&l1,&l2)==7);
  184.     assert(c1=='y'); assert(c2=='n');
  185.     assert(s1==-4201); assert(n1==0123); assert(n2==0xface);
  186.     assert(l1==42798L); assert(l1==0123456L); assert(l2==3400727262L); assert(l2==0xcab2fadeL);
  187.  
  188.     f1=f2=f3=0.0;
  189.     assert(scanf("%f;%e;%g;",&f1,&f2,&f3)==3);
  190.     assert(roughly(f1,2.67e-283)); assert(roughly(f2,3.14)); assert(roughly(f3,2.718e59));
  191.     assert(fclose(stdin)==0);
  192.     memcpy(stdin,&tempin,sizeof(FILE));
  193.     printf("passed printf/scanf tests...\n");
  194.  
  195.     printf("==> finished xpfsf <==\n");
  196.     return;
  197. }
  198.